package com.cn.transfer.jobhandler.duplicates;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cn.transfer.common.constant.KeyConstant;
import com.cn.transfer.common.entity.MainFormData;
import com.cn.transfer.common.entity.VvSaSaleOrder;
import com.cn.transfer.common.http.ApiClient;
import com.cn.transfer.common.utils.ConnmeUtils;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author: weidian
 * @date: 2024/1/4 17:44
 * @Description: 去重采购订单
 */
@Component
public class DuplicatesPurchaseXxlJob {
    @XxlJob("DuplicatesPurchase")
    public void duplicatesPurchase(){
        List<MainFormData> mainFormData = new ArrayList<>();
        Header[] headers = ConnmeUtils.createHeaders();
        Map<String, Object> parameter = new HashMap<>();
        parameter.put("app_id", "64f00ef131adb70007ad6e6d");
        parameter.put("entry_id", "64f00f0e68761f0007719cf9");
        boolean fale=true;
        String zh= "";
        do {
            if (!zh.equals("")){
                parameter.put("data_id", zh);
            }
            parameter.put("limit", 100);
            String jsonSale = JSONUtil.toJsonStr(parameter);
            JSONObject jdySale = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/list", headers, jsonSale);
            String data = jdySale.getStr("data");
            JSONArray objects = JSONUtil.parseArray(data);
            List<MainFormData> mainFormDataList = JSONUtil.toList(objects, MainFormData.class);
            if (mainFormDataList.size()==100){
                zh=mainFormDataList.get(mainFormDataList.size()-1).get_id();
            }else {
                fale=false;
            }
            mainFormData.addAll(mainFormDataList);
        }while (fale);
        Set<String> uniqueSet = new HashSet<>();
        List<String> duplicates = new ArrayList<>();

        for (MainFormData mainFormData1 : mainFormData) {
            if (!uniqueSet.add(mainFormData1.getDjbh())) {
                duplicates.add(mainFormData1.get_id());
            }
        }
        for (String duplicate : duplicates) {
            Map<String, Object> parameterDelete = new HashMap<>();
            parameterDelete.put("app_id", "64f00ef131adb70007ad6e6d");
            parameterDelete.put("entry_id", "64f00f0e68761f0007719cf9");
            parameterDelete.put("data_id", duplicate);
            String jsonSaleDelete = JSONUtil.toJsonStr(parameterDelete);
            JSONObject jdySaledelete = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/delete", headers, jsonSaleDelete);
        }
    }
}
